package com.xy.controller;

import cn.hutool.core.util.IdUtil;
import com.xy.config.CustomRedisKeyGenerator;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xxy
 * @date 2022/1/7 18:54
 */
@RestController
public class UseRedisSpringCacheController {


    @GetMapping("test")
    @Cacheable(value = "test", key = "#root.methodName")
    public Object test() {
        return "test";
    }

    @GetMapping("test2")
    @Cacheable(value = "test2", key = "#root.methodName")
    public Object test2() {
        return "test";
    }

    @GetMapping("test3")
    @Cacheable(value = "test3", keyGenerator = "customRedisKeyGenerator")
    public Object test3() {
        return "test";
    }

    @DeleteMapping("delete")
    @CacheEvict(value = "test")
    public Object delete() {
        return "delete";
    }

    @RequestMapping("create")
    @CachePut(value = "test", key = "#result.id")
    public Object create() {
        Person person = new Person();
        String uuid = IdUtil.randomUUID();
        person.setId(uuid);
        return person;
    }


    class Person {
        private String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }


}
